package love.yuanmeng.guetcoalsystem.service.impl;
/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:CHJY创幻
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/9/26 12:23
 */

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import love.yuanmeng.guetcoalsystem.mapper.UserMapper;
import love.yuanmeng.guetcoalsystem.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import love.yuanmeng.guetcoalsystem.mapper.UserMapper;
import love.yuanmeng.guetcoalsystem.pojo.Message;
import love.yuanmeng.guetcoalsystem.pojo.News;
import love.yuanmeng.guetcoalsystem.pojo.User;
import love.yuanmeng.guetcoalsystem.service.MessageService;
import love.yuanmeng.guetcoalsystem.service.UserService;
import love.yuanmeng.guetcoalsystem.util.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Autowired
    MessageService messageService;

    @Override
    public DataGridView getAllUser() {
        DataGridView data = new DataGridView();
        data.setData(userMapper.selectList(new QueryWrapper<>()));
        return data;
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateUser(User user) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", user.getId())
                .set("username", user.getUsername())
                .set("userpaw", user.getUserpaw())
                .set("phone", user.getPhone());
        return userMapper.update(null, wrapper);
    }

    @Override
    public int deleteUser(User user) {
        return userMapper.deleteById(user.getId());
    }

    @Override
    public DataGridView getUsersOrderBy(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (user.getId() != 0) {
            wrapper.eq("id", user.getId());
        }
        if (!"".equals(user.getUsername())) {
            wrapper.eq("username", user.getUsername());
        }
        if (!"".equals(user.getPhone())) {
            wrapper.eq("phone", user.getPhone());
        }
        if (!"".equals(user.getIdentity())) {
            wrapper.eq("identity", user.getIdentity());
        }
        DataGridView data=new DataGridView();
        data.setData(userMapper.selectList(wrapper));
        return data;
    }

    @Override
    public IPage getTradeUser(Page page, User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.in(StringUtils.isBlank(user.getStatus()),"status", new String[]{"0", "1"});
        queryWrapper.eq(!StringUtils.isBlank(user.getStatus()),"status", user.getStatus());
        queryWrapper.in(StringUtils.isBlank(user.getAvailable()),"available", new String[]{"待审核","过审核","未过审"});
        queryWrapper.like(!StringUtils.isBlank(user.getUsername()), "username", user.getUsername())
                .eq(!StringUtils.isBlank(user.getAvailable()), "available", user.getAvailable());
        return userMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean approvedUser(User user, Message message) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("enterpriseid",user.getEnterpriseid());
        return userMapper.update(user, queryWrapper)==1&&messageService.inserMessage(message);
    }
}
